Pandas dataframe overwrite object [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to overwrite the matrix I have of dimensions n to matrix of dimensions m (n>m). Intuitive code like this does not work:
sigmaSmall = sigmaSmall.loc[indices, indices]
How can I do it in 1 line?

The 2nd dimension takes column names, not numbered indices.
So instead do:
sigmaSmall = sigmaSmall.loc[indices, sigmaSmall.columns[indices]]

Not knowing what your indicies are make it hard to tell, but it should look something like this
df = pd.DataFrame([[1,2,3],[1,2,3],[1,2,3]], columns=['a','b','c'])
df.loc[0:1, ['a','b']]
Where the second argument is the column names that you want to select

Related

Split a string into two in solidity [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I have a list of words like below
["HEllO","HI","GREAT"]
and another list of tuples
[("HELLO",123),("HI",2134),("HELLO",65)]
If all the words in first list comes in second list atleast once then I need True as outcome else False.
Python version (based on comments from
Pranav Hosangadi and matszwecja)
a = ["HEllO","HI","GREAT"]
b = [("HELLO",123),("HI",2134),("HELLO",65)]
not (set(a) - set(x[0] for x in b))

Using Regex to drop period from many column names [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'd like to drop a '.' from a column name using regex, and want the code to be applied to many column names that end in '.', so that each pair of like-named columns can be merged into one.
For example, the column names 'Fund' and 'Fund.' are different and have different values, but should become just 'Fund'.
What would be the best regex to use for this?
Try this:
df = pd.DataFrame([1], columns=['Fund.'])
df.columns = df.columns.str.replace('.','')
Output:
print(df.columns)
Index(['Fund'], dtype='object')

for-loop to retrieve and store variables in a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I retrieve several dataframes from spreadsheets of an Excel file.
I would like to store these dataframes into a list so that I can concatenate the dataframes into one dataframe.
However, how can I store the variables themselves instead of their them.
These are the data frames that I created.
df0120
df0220
df0320
df0419
df0519
df0619
df0719
df0819
df0919
df_lst = list()
for name in dir():
if name.startswith('df0'):
df_lst.append(name)
print(df_lst)
My results
['df0120', 'df0220'...]
Expected results
[df0120, df0220 ...]
What you see is how Python prints a list of strings in the built-in way, by itself. But, you can print it yourself in another way if you want:
print('['+', '.join(df_lst)+']')

Can I change the dtype of mean function in Pandas? I want to change it to int type [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can I change the dtype of mean function in Pandas?
I will get the value in float type, but I specifically do not want to include the after decimal values,I want it only on integers, can anyone help me on this?
You can then apply a transformation to the result.
df['mean'] = df['mean'].apply(lambda x: round(x))
or, if you want to truncate:
df['mean'] = df['mean'].apply(lambda x: int(x))

How to do pandas column selection while getting code completion? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
When working with pandas, I often use name based column indexing. E.g:
df = pd.DataFrame({"abc":[1,2], "bde":[3,4], "mde":[3,4]})
df[["mde","bde"]]
As I have longer column names it because easy for me to create a typo in the column names since they are strings and no code completion. It'd be great if I could do something like:
df.SelectColumnsByObjectAttributeNotString([df.mde, df.bde])
IIUC, you can use name attribute.
df = pd.DataFrame({"a":[1,2], "b":[3,4]})
columns = [df.a.name, df.b.name]
columns
['a', 'b']
I think you may be looking for:
df.columns.values.tolist()

Categories

Resources