简体   繁体   中英

How to convert columns of a dataframe to list with different lengths?

I have a data frame with columns of different lengths. It looks like this:

Croptypes   VariableInputs   FixedInputs
barley      fertilizer       land
rapeseed                     labor
wheat                        capital

when I list the elements from the columns by the following codes:

Croptypes = list(df['Croptypes'])
VariableInputs = list(df['VariableInputs'])

I get this results.

['barley', 'rapeseed', 'wheat']
['fertilizer', nan, nan]

How can I tell "list" not to list the empty cells?

I expect:

['barley', 'rapeseed', 'wheat']
['fertilizer']

Use dropna and tolist :

>>> df['VariableInputs'].dropna().tolist()
['fertilizer']
>>> 

Or for every column:

print({k: v.dropna().tolist() for k, v in df.to_dict('s').items()})

Output:

{'Croptypes': ['barley', 'rapeseed', 'wheat'], 'VariableInputs': ['fertilizer'], 'FixedInputs': ['land', 'labor', 'capital']}

You can create dictionary for each column with values as list as:

d = {col:df[col].dropna().tolist() for col in df}

d
{'Croptypes': ['barley', 'rapeseed', 'wheat'],
 'VariableInputs': ['fertilizer'],
 'FixedInputs': ['land', 'labor', 'capital']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM