简体   繁体   中英

How to split up two CSV file columns in a new column, showing matches in pandas, using dataframes?

I am trying to clean up a CSV file data set before I use it to make a couple of dash graphs.

One of the columns is UNITMEASURENAME and includes:

Thousand Barrels per day (kb/d)
Thousand Kilolitres (kl)
Thousand Barrels per day (kb/d)
Thousand Kilolitres (kl)
Conversion factor barrels/ktons
Conversion factor barrels/ktons
Thousand Barrels (kbbl)

Another column contains the value for each of the corresponding rows .

There is also a country and a data column.

What I need to do is split up the UNITMEASURENAME into separate columns, taking the values from the column with the numbers.

Would df.pivot_table work?

I have done the following in pandas , but I don't think it will working within Dash for a plotly graph:

TK = df.loc[df['UNITMEASURENAME']=='Thousand Kilolitres (kl)']

IN = df.loc[df['COUNTRYNAME']=='INDIA']

This isn't making a new colum in the actual CSV file.

TK = df.loc[df['UNITMEASURENAME']=='Thousand Kilolitres (kl)']

IN = df.loc[df['COUNTRYNAME']=='INDIA']

I want new columns and then I will save the actual CSV file with them.

{'Unnamed: 0': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4},
 'Year': {0: 2018, 1: 2018, 2: 2018, 3: 2018, 4: 2018},
 'Month': {0: 3, 1: 3, 2: 3, 3: 4, 4: 4},
 'OBSVALUE': {0: 7323.0, 1: 9907.0, 2: 48827.7847, 3: 9868.0, 4: 47066.6794},
 'COUNTRYNAME': {0: 'SAUDI ARABIA',
  1: 'SAUDI ARABIA',
  2: 'SAUDI ARABIA',
  3: 'SAUDI ARABIA',
  4: 'SAUDI ARABIA'},
 'UNITMEASURENAME': {0: 'Conversion factor barrels/ktons',
  1: 'Thousand Barrels per day (kb/d)',
  2: 'Thousand Kilolitres (kl)',
  3: 'Thousand Barrels per day (kb/d)',
  4: 'Thousand Kilolitres (kl)'},
 'alternate_date': {0: '2018-03-01',
  1: '2018-03-01',
  2: '2018-03-01',
  3: '2018-04-01',
  4: '2018-04-01'}}

Header for CSV file:

Unnamed: 0  Year    Month   OBSVALUE    COUNTRYNAME UNITMEASURENAME alternate_date
0   0   2018    3   7323.0000   SAUDI ARABIA    Conversion factor barrels/ktons 2018-03-01
1   1   2018    3   9907.0000   SAUDI ARABIA    Thousand Barrels per day (kb/d) 2018-03-01
2   2   2018    3   48827.7847  SAUDI ARABIA    Thousand Kilolitres (kl)    2018-03-01
3   3   2018    4   9868.0000   SAUDI ARABIA    Thousand Barrels per day (kb/d) 2018-04-01
4   4   2018    4   47066.6794  SAUDI ARABIA    Thousand Kilolitres (kl)    2018-04-01

It seems that you have a multi-column key (year, month, country name, and maybe alternate_date), which is fine, but it would make pivoting difficult/dangerous.So, I will simply give you some code to create new columns based on the values in that one column.

First, I love to copy a dataframe so that I'm not losing my original data

dfc = df.copy()

Now, let's get a unique list of all the values of that column

vals = dfc['UNITMEASURENAME'].values
vals = np.unique(vals)

Now let's create a new column for each of the values

for val in vals:
    dfc[val] = dfc.apply(lambda x: x['OBSVALUE'] if x['UNITMEASURENAME'] == val else None , axis = 1)

if lambda functions are too confusing:

dfc = df.copy()
vals = dfc['UNITMEASURENAME'].values
vals = np.unique(vals)

def fun(row):
    if row['UNITMEASURENAME'] == val:
        return row['OBSVALUE']
    else:
        return None

for val in vals:
    dfc[val] = dfc.apply(fun, axis = 1)

I tested this code.

I think you could use pivot method of Pandas DataFrame to create new columns using categorical values.

df = ... # your dataframe

# We keep 'Unnamed: 0' column as index for later when we merge df and df2
df2 = df.pivot(index='Unnamed: 0', columns='UNITMEASURENAME', values=['OBSVALUE'])

# df2 is a MultiIndex dataframe.. So we access the level needed and then reset_index
df2 = df2['OBSVALUE'].reset_index()

Now you can merge this to the original dataframe to keep other columns for your analysis

final_df = pd.merge(df, df2, on='Unnamed: 0')

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