Skip to content
Advertisement

Tag: dataframe

How to move a column in a pandas dataframe

I want to take a column indexed ‘length’ and make it my second column. It currently exists as the 5th column. I have tried: I see the following error: TypeError: must be str, not list I’m not sure how to interpret this error because it actually should be a list, right? Also, is there a general method to move any

Delete rows that do not contain specific text

I have a tabular file that looks like this: I’m trying to create a script to go through and delete the entire row if column 2 (‘KEGG_KOs’) does not begin with ‘K0’. I’m trying to create an output of: Previous responses have referred people to pandas DataFrame but I’ve had no luck using those responses to help. Any would be

Pandas: Conditionally replace values based on other columns values

I have a dataframe (df) that looks like this: Now my goal is for each add_rd in the event column, the associated NaN-value in the environment column should be replaced with a string RD. What I did so far I stumbled across df[‘environment’] = df[‘environment].fillna(‘RD’) which replaces every NaN (which is not what I am looking for), pd.isnull(df[‘environment’]) which is

How to add values to a new column in pandas dataframe?

I want to create a new named column in a Pandas dataframe, insert first value into it, and then add another values to the same column: Something like: How do I do that? Answer Dont do it, because it’s slow: updating an empty frame a-single-row-at-a-time. I have seen this method used WAY too much. It is by far the slowest.

ValueError: DataFrame constructor not properly called

I am trying to create a dataframe with Python, which works fine with the following command: but, when I try to get the data from a variable instead of hard-coding it into the data argument; eg. : I expect this is the same and it should work? But I get: Answer Reason for the error: It seems a string representation

Advertisement