如何设置一个 pandas 数据框,第一个值是什么?
How can I set a pandas dataframe with the first value is what stays?
我得到以下 table 每个人都有重复值:
Customer
Jan
Feb
Mar
Apr
Jun
Jul
Adam
345
345
345
345
345
Susan
645
645
645
645
Paul
153
153
153
153
可以看到第一个值是实际值。所以 table 应该是这样的:
Costumer
Jan
Feb
Mar
Apr
Jun
Jul
Adam
345
Susan
645
Paul
153
解决此问题的更好方法是什么table?
数据:
import numpy as np
import pandas as pd
data = {'Customer': ['Adam', 'Susan', 'Paul'],
'Jan': [345.0, np.NaN, np.NaN],
'Feb': [345.0, np.NaN, 153.0],
'Mar': [345.0, 645.0, 153.0],
'Apr': [345.0, 645.0, 153.0],
'Jun': [345.0, 645.0, 153.0],
'Jul': [np.NaN, 645.0, np.NaN]}
df = pd.DataFrame(data)
您可以 mask
重复值:
out = (df.mask(df.apply(lambda x: x.duplicated(), axis=1)).fillna(''))
输出:
Customer Jan Feb Mar Apr Jun Jul
0 Adam 345.0
1 Susan 645.0
2 Paul 153.0
我得到以下 table 每个人都有重复值:
Customer | Jan | Feb | Mar | Apr | Jun | Jul |
---|---|---|---|---|---|---|
Adam | 345 | 345 | 345 | 345 | 345 | |
Susan | 645 | 645 | 645 | 645 | ||
Paul | 153 | 153 | 153 | 153 |
可以看到第一个值是实际值。所以 table 应该是这样的:
Costumer | Jan | Feb | Mar | Apr | Jun | Jul |
---|---|---|---|---|---|---|
Adam | 345 | |||||
Susan | 645 | |||||
Paul | 153 |
解决此问题的更好方法是什么table?
数据:
import numpy as np
import pandas as pd
data = {'Customer': ['Adam', 'Susan', 'Paul'],
'Jan': [345.0, np.NaN, np.NaN],
'Feb': [345.0, np.NaN, 153.0],
'Mar': [345.0, 645.0, 153.0],
'Apr': [345.0, 645.0, 153.0],
'Jun': [345.0, 645.0, 153.0],
'Jul': [np.NaN, 645.0, np.NaN]}
df = pd.DataFrame(data)
您可以 mask
重复值:
out = (df.mask(df.apply(lambda x: x.duplicated(), axis=1)).fillna(''))
输出:
Customer Jan Feb Mar Apr Jun Jul
0 Adam 345.0
1 Susan 645.0
2 Paul 153.0